package com.reminiscence.main;

import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.reminiscence.classes.Utente;
import com.reminiscence.database.OperazioniDB;

public class RicercaUtente extends Activity
{

	Button btnCerca;
	
	EditText edtNome;
	EditText edtCognome;
	EditText edtData;
	EditText edtLuogo;
	ListAdapter adapter;
	
	public static Utente utenteRic = new Utente();
	public static ListView lw;

	public static ArrayList<HashMap<String, String>> listaUtenti = new ArrayList<HashMap<String, String>>();

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ricerca_utente);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		
		edtNome = (EditText)findViewById(R.id.edtRicNome);
		edtCognome = (EditText)findViewById(R.id.edtRicCognome);
		edtData = (EditText)findViewById(R.id.edtRicDataN);
		edtLuogo = (EditText)findViewById(R.id.edtLuogoN);
		
		lw = (ListView)findViewById(R.id.lstRUList);
		
		Tw tw = new Tw();
		edtNome.addTextChangedListener(tw);
		edtCognome.addTextChangedListener(tw);
		edtData.addTextChangedListener(tw);
		edtLuogo.addTextChangedListener(tw);

		ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
		MainActivity.utente.nome = "davide";
		params.add(new BasicNameValuePair("IdUtente", MainActivity.utente.nomeUtente));
		Boolean ret = false; 
		try
		{
			ret =  (Boolean)(new OperazioniDB(this, "listaUtenti", params).execute().get());
			
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		ListView lw = (ListView)findViewById(R.id.lstRUList);
		lw.setAdapter(null);
		
		this.adapter = new SimpleAdapter
		(
			this, listaUtenti,
			R.layout.list_item_utenti,
			new String[]
			{
				"nome",
				"cognome",
				"dataN",
				"luogoN",
				"provincia",
				"nomeUtente"
			},
			new int[]
			{
				R.id.txtListaUtentiNome,
				R.id.txtListaUtentiCognome,
        		R.id.txtListaUtentiDataNascita,
        		R.id.txtListaUtentiLuogoNascita,
        		R.id.txtListaUtentiProvincia,
        		R.id.txtListaUtentiNomeUtente
    		}
		);

		lw.setAdapter(adapter);
		
		lw.setOnItemClickListener
		(
			new AdapterView.OnItemClickListener()
			{

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
				{
					View curr;
					TextView tw;
					
					int k = RicercaUtente.lw.getCount();
					
					String idArray[] = new String[k];
					for (int i = 0; i< k; i++)
					{
						curr = arg0.getChildAt(i);
						tw = (TextView)curr.findViewById(R.id.txtListaUtentiNomeUtente);
						idArray[i] = tw.getText().toString();
					}
					
					curr = arg0.getChildAt((int) arg3);
					tw = (TextView)curr.findViewById(R.id.txtListaUtentiNomeUtente);
					String s = tw.getText().toString();
			
					Intent i = new Intent(RicercaUtente.this, ProfiloAmico.class);
					i.putExtra("IdUtente", s);
					startActivity(i);	
				}
			}
		);
	}
	
	public boolean onCreateOptionsMenu(Menu menu)
	{
		getMenuInflater().inflate(R.menu.ricerca_utente, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		if(item.getItemId() == R.id.mnuRUProfilo)
		{
			Intent i = new Intent(this, ProfiloCompleto.class);
			i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(i);
			
		}
		return false;
	}
	
	class Tw implements TextWatcher
	{

		@Override
		public void afterTextChanged(Editable arg0) {
			// TODO Auto-generated method stub
			((SimpleAdapter) RicercaUtente.this.adapter).getFilter().filter(arg0);
		}

		@Override
		public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {
			// TODO Auto-generated method stub
			
		}
		
	}
}
